package com.kzd.gtgame.main

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.*
import com.kzd.gtgame.R
import com.kzd.gtgame.app.URL_REBATE
import com.kzd.gtgame.app.SPSConstance.uid
import com.kzd.gtgame.app.SPSConstance.userInfo
import com.kzd.gtgame.app.SPSConstance.userMine
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.UserInfo
import com.kzd.gtgame.entity.UserMine
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.mine.*
import com.kzd.gtgame.utils.httpSign1
import com.kzd.gtgame.utils.httpTime
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_mine.*
import java.lang.StringBuilder

class MainMineFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        fun newInstance(): MainMineFragment {
            return MainMineFragment()
        }
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_mine

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(llMainMineBar)

        //消息中心
        ivMainMineMessage.setOnClickAntiShakeListener {
            toMineMessageActivity()
        }

        //设置中心
        ivMainMineSetting.setOnClickAntiShakeListener {
            toMineSettingActivity()
        }

        //用户中心
        clMainMineUserInfo.setOnClickAntiShakeListener {
            toMineUserActivity()
        }

        //会员
        clMainMineMember.setOnClickAntiShakeListener {
            toMineMemberActivity(index = 1)
        }

        //我的积分
        clMainMineIntegral.setOnClickAntiShakeListener {
            toMainActivity(index = 2)
        }

        //平台币
        clMainMineCurrency.setOnClickAntiShakeListener {
            toMinePlatformActivity()
        }

        //申请返利
        tvMainMineRebate.setOnClickAntiShakeListener {
            val userId = userInfo?.user_id ?: ""
            val url = StringBuilder(URL_REBATE)
                .append("&uid=").append(userId)
                .append("&t=").append(httpTime)
                .append("&sign=").append(httpSign1(userId))
                .toString()
            toMineRichTextActivityUrl(title = "返利中心", url = url, isLogin = true)
        }

        //收藏中心
        tvMainMineCoupon.setOnClickAntiShakeListener {
            toMineCouponActivity()
        }

        //收藏中心
        tvMainMineCollect.setOnClickAntiShakeListener {
            toMineCollectActivity()
        }

        //我的礼包
        tvMainMineGiving.setOnClickAntiShakeListener {
            toMineGiftActivity()
        }

        //我的足迹
        tvMainMineFootprint.setOnClickAntiShakeListener {
            toMineFootprintActivity()
        }

        //充值转游
        tvMainMineTransfer.setOnClickAntiShakeListener {
            toMineTransferActivity()
        }

        //邀请好友
        tvMainMinInvite.setOnClickAntiShakeListener {
            toMineInviteActivity()
        }

        //小号回收
        tvMainMineSmall.setOnClickAntiShakeListener {
            toBargainBuyActivity()
        }

        //福利
        ivMainMineWelfare.setOnClickAntiShakeListener {
            toMineMemberActivity(index = 0)
        }

        //我的账单
        tvMainMineBill.setOnClickAntiShakeListener {
            toMineRecordBillActivity()
        }

        //投诉反馈
        tvMainMineFeedback.setOnClickAntiShakeListener {
            toMineFeedbackActivity()
        }

        //下载管理
        tvMainMineDownload.setOnClickAntiShakeListener {
            toMineDownloadActivity()
        }

        //账号安全
        tvMainMineAccount.setOnClickAntiShakeListener {
            toMineUserActivity()
        }

        //加入qq
        tvMainMineJoinQQ.setOnClickAntiShakeListener {
            toExternalAppQQ(requireActivity())
        }
    }


    override fun initData() {
        viewModel.userAppTips()
    }

    override fun onResume() {
        super.onResume()
//        if (checkLogin() && userInfo == null) {
//            viewModel.userInfo()
//        }
//        if (checkLogin() && userMine == null) {
//            viewModel.userMine()
//        }
        fillData(userInfo, userMine)
        viewModel.userInfo()
        viewModel.userMine()
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data, userMine)
        })
        viewModel.userMineLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(userInfo, data)
        })

        BaseViewModel.userAppTipsLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.msg_rs != 1) {
                viewMainMineMessageRead.visibility = View.GONE
            } else {
                viewMainMineMessageRead.visibility = View.VISIBLE
            }
        })
    }

    private fun fillData(userInfo: UserInfo?, userMine: UserMine?) {

        ivMainMineUserHeader.loadCircleImage(
            if (userInfo?.face.isNullOrEmpty())
                R.mipmap.ic_test_1 else userInfo?.face ?: R.mipmap.ic_test_1
        )

        if (uid.isNullOrEmpty()) {
            tvMainMineUserPhone.visibility = View.GONE
        } else {
            tvMainMineUserPhone.visibility = View.VISIBLE
        }

        tvMainMineUserNickname.text = when {
            uid.isNullOrEmpty() -> "未登录"
            userInfo?.pet_name.isNullOrEmpty() -> userInfo?.user_name
            else -> userInfo?.pet_name
        }

        tvMainMineUserPhone.text =
            if (!userInfo?.mobile.isNullOrEmpty()) userInfo?.mobile else "未绑定"

        tvMainMineMoneyPlatform.text = userMine?.coin ?: "0"
        tvMainMineMoneyIntegral.text = userMine?.points ?: "0"
    }
}